import java.util.Stack;

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public static ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode result=null;
        Stack<ListNode> s1 = new Stack<>();
        Stack<ListNode> s2 = new Stack<>();

        while(pHead1!=null){
            s1.add(pHead1);
            pHead1 = pHead1.next;
        }
        while(pHead2!=null){
            s2.add(pHead2);
            pHead2 = pHead2.next;
        }

        while(!s1.isEmpty() && !s2.isEmpty() && s1.peek()==s2.peek()){
            result = s1.pop();
            s2.pop();
        }

        return result;
    }
}